package com.bell.atensettings.settingDetails;

import java.util.ArrayList;
import java.util.List;

import com.bell.atensettings.R;
import com.bell.atensettings.bean.DisplayItems;

import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageManager;
import android.media.AudioManager;
import android.os.Bundle;
import android.provider.Settings;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodInfo;
import android.view.inputmethod.InputMethodManager;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

public class VoiceSettings extends Activity implements OnItemClickListener
{
    private ArrayList<DisplayItems> _mItem;
    private boolean flagDTMF;
    private ItemAdapter itemAdapter;
    private ListView lv;
    private TextView mTitleView;
    private AudioManager mAudioManager;
    private boolean systemVolume;
    
    private void initItem()
    {
        String[] asVoice = getResources().getStringArray(R.array.voice);
        DisplayItems item1 = new DisplayItems();
        DisplayItems item2 = new DisplayItems();
        
        item1.setName(asVoice[0]);
        item2.setName(asVoice[1]);
        _mItem.add(item1);
        _mItem.add(item2);
    }
    
    private void initListItem()
    {
        _mItem = new ArrayList<DisplayItems>();
        initItem();
        itemAdapter = new ItemAdapter(_mItem);
        lv.setAdapter(itemAdapter);
        lv.setOnItemClickListener(this);
    }
    
    private boolean getSilentModeSettingValue()
    {
        return mAudioManager.getStreamVolume(1) > 0;
    }
    
    private void setSilentSettingValue(boolean isSilent)
    {
        if (!isSilent)
            mAudioManager.setStreamVolume(AudioManager.STREAM_MUSIC, 0, AudioManager.FLAG_PLAY_SOUND );
        else
            mAudioManager.setStreamVolume(AudioManager.STREAM_MUSIC, 100, AudioManager.FLAG_PLAY_SOUND );
    }
    
    @Override
    public void onBackPressed()
    {
        super.onBackPressed();
        overridePendingTransition(R.anim.start, R.anim.push_left_out);
    }

    @Override
    protected void onCreate(Bundle bundle)
    {
        super.onCreate(bundle);
        setContentView(R.layout.public_settings);
        
        mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
        flagDTMF = false;
        
        if (Settings.System.getInt(getContentResolver(), "sound_effects_enabled", 1) == 1)
        {
            flagDTMF = true;
        }
        
        if (mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC) == 0)
        {
            systemVolume = false;
        }
        else
        {
            systemVolume = true;
        }
        
        mTitleView = (TextView) findViewById(R.id.setting_title);
        lv = (ListView) findViewById(R.id.listview);
        
        mTitleView.setText(getIntent().getStringExtra("title"));
        initListItem();
        
        InputMethodManager mimm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        List<InputMethodInfo> mImis = mimm.getInputMethodList();

        for (int i = 0; i < mImis.size(); i++)
        {
            PackageManager pm = getPackageManager();
            Log.i("Android InputMethod: ", "default_input_method: " + Settings.Secure.getString(getContentResolver(), "default_input_method"));
            Log.i("Android InputMethod: ", "id: " + mImis.get(i).getId() + "label: " + mImis.get(i).loadLabel(pm));
        }
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id)
    {
        ImageView tagView = (ImageView) view.getTag();
        switch (position)
        {
            case 0:
                if (systemVolume)
                {
                    tagView.setImageResource(R.drawable.unright_icon);
                    systemVolume = false;
                }
                else
                {
                    tagView.setImageResource(R.drawable.right_icon);
                    systemVolume = true;
                }
                setSilentSettingValue(systemVolume);
                break;
            case 1:
                if (flagDTMF)
                {
                    Settings.System.putInt(getContentResolver(), "sound_effects_enabled", 0);
                    tagView.setImageResource(R.drawable.unright_icon);
                    flagDTMF = false;
                }
                else
                {
                    Settings.System.putInt(getContentResolver(), "sound_effects_enabled", 1);
                    tagView.setImageResource(R.drawable.right_icon);
                    flagDTMF = true;
                }

            default:
                break;
        }
    }
    
    public class ItemAdapter extends BaseAdapter
    {
        private ArrayList<DisplayItems> mItem;

        public ItemAdapter(ArrayList<DisplayItems> items)
        {
            this.mItem = items;
        }

        public int getCount()
        {
            return this.mItem.size();
        }

        public Object getItem(int position)
        {
            return this.mItem.get(position);
        }

        public long getItemId(int position)
        {
            return position;
        }

        public View getView(int position, View convertView, ViewGroup parent)
        {
            DisplayItems dispItem = (DisplayItems) this.mItem.get(position);

            if (convertView == null)
                convertView = ((LayoutInflater) getSystemService("layout_inflater")).inflate(
                        R.layout.list_display_item, null);
            convertView.setBackgroundResource(R.drawable.listnormal);
            ImageView signView = (ImageView) convertView.findViewById(R.id.img_sign);
            TextView tv_prop = (TextView) convertView.findViewById(R.id.tv_data);
            
            switch (position)
            {
                case 0:
                    if (systemVolume)
                        signView.setImageResource(R.drawable.right_icon);
                    else
                        signView.setImageResource(R.drawable.unright_icon);
                    break;
                case 1:
                    if (flagDTMF)
                        signView.setImageResource(R.drawable.right_icon);
                    else
                        signView.setImageResource(R.drawable.unright_icon);
                    break;
                default:
                    break;
            }
            tv_prop.setText(dispItem.getName());
            convertView.setTag(signView);
            return convertView;
        }
    }
}
